package com.zhao.reggie3.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.reggie3.damain.Dish;
import com.zhao.reggie3.damain.DishFlavor;
import com.zhao.reggie3.dao.DishDao;
import com.zhao.reggie3.dto.DishDto;
import com.zhao.reggie3.service.DishFlavorService;
import com.zhao.reggie3.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: zhao
 * @time: 2022/8/17 17:17
 */
@Service
@Slf4j
public class DishServiceimpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * @description 新增菜品信息
     * @param
     * @return
     **/
    @Override
    @Transactional
    public void savewithflavor(DishDto dishDto) {
        //保存菜品基本信息
        this.save(dishDto);
        //获取保存的菜品id
        Long dishid = dishDto.getId();
        //获取口味列表,
        List<DishFlavor> flavors = dishDto.getFlavors();

        List<DishFlavor> collect = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishid);
            return flavor;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(collect);
    }

    @Override
    public void updatewithflavor(DishDto dishDto) {
        //保存菜品基本信息
        this.save(dishDto);
        //获取保存的菜品id
        Long dishid = dishDto.getId();
        //获取口味列表,
        List<DishFlavor> flavors = dishDto.getFlavors();

        List<DishFlavor> collect = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishid);
            return flavor;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(collect);
    }
}